package com.yyh.LIst1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Test1 {
    public static void main(String[] args) {
        //创建集合对象
        List l = new ArrayList();
        //添加元素
        l.add("hello");
        l.add(10);
        l.add(true);
        l.add('a');
        l.add(3.14);
        System.out.println("l = " + l);//l = [hello, 10, true, a, 3.14]
        //在指定索引位置添加元素
        l.add(1, "world");
        System.out.println("l = " + l);//l = [hello, world, 10, true, a, 3.14]
        //删除元素
        Integer index = (Integer) l.indexOf("world");
        l.remove(index);
        System.out.println("l = " + l);//l = [hello, world, 10, true, a, 3.14]
        //删除指定元素
        l.remove("world");
        System.out.println("l = " + l);//l = [hello, 10, true, a, 3.14]
        //批量删除
        List<Double> l2 = Arrays.asList(3.14);
        l.removeAll(l2);
        System.out.println("l = " + l);//l = [hello, 10, true, a]
        //修改元素
        l.set(0, "java");
        System.out.println("l = " + l);//l = [java, 10, true, a]
        //查询元素
        Object element = l.get(0);
        System.out.println("element = " + element);//element = java
        //线性查找：从前往后
        int index1 = l.indexOf("java");
        System.out.println("index1 = " + index1);//index1 = 0
        //线性查找：从后往前
        int index2 = l.lastIndexOf("java");
        System.out.println("index2 = " + index2);//index2 = 0
        //获取迭代器
        Iterator it = l.iterator();

    }
}
